package com.ddyx.service.impl;

import java.util.Date;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddyx.entity.EventLog;
import com.ddyx.entity.EventPostLike;
import com.ddyx.mapper.EventLogMapper;
import com.ddyx.mapper.EventPostLikeMapper;
import com.ddyx.mapper.PostMapper;
import com.ddyx.service.IEventPostLikeService;
import com.ddyx.util.EventTag;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class EventPostLikeServiceImpl implements IEventPostLikeService{

	@Resource
	EventPostLikeMapper eplMapper;
	@Resource
	EventLogMapper elMapper;
	@Resource
	PostMapper postMapper;
	
	@Transactional
	@Override
	public ResponseForm addPostLike(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null!=param.getData()) {
			try {
				Map paramMap = (Map) param.getData();
				Date date = new Date();
				EventPostLike epl = new EventPostLike();
				MyBeanUtils.populate(epl, paramMap);
				epl.setEventTime(date);
				eplMapper.insertSelective(epl);
				
				//点赞数加一
				postMapper.addLikeCount(epl.getPostId());
				
				EventLog eventLog = new EventLog();
				eventLog.setEventTag(EventTag.CLICKLIKEPOST);
				eventLog.setEventTime(date);
				eventLog.setStoreNo(paramMap.get("storeNo")+"");
				eventLog.setWxUid(paramMap.get("wxUid")+"");
				elMapper.insertSelective(eventLog);
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Transactional
	@Override
	public ResponseForm cancelPostLike(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null!=param.getData()) {
			try {
				Map paramMap = (Map) param.getData();
				Date date = new Date();
				EventPostLike epl = new EventPostLike();
				MyBeanUtils.populate(epl, paramMap);
				eplMapper.delete(epl);
				
				//点赞数减一
				postMapper.reduceLikeCount((int)paramMap.get("postId"));
				
				EventLog eventLog = new EventLog();
				eventLog.setEventTag(EventTag.CANCELLIKEPOST);
				eventLog.setEventTime(date);
				eventLog.setStoreNo(paramMap.get("storeNo")+"");
				eventLog.setWxUid(paramMap.get("wxUid")+"");
				elMapper.insertSelective(eventLog);
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}
	
}
